import { nextTick } from '../utils'

let queue = []
let has = {}
let pending = false

function flushSchedulerQueue() {
  queue.forEach((wathcer) => wathcer.run())
  has = {}
  queue = []
  pending = false
}
export function queueWatcher(wathcer) {
  let id = wathcer.id
  if (has[id] == null) {
    // 去重
    has[id] = true
    queue.push(wathcer)
  }
  if (!pending) {
    // 防抖，执行一次
    nextTick(flushSchedulerQueue)
    pending = true
  }
}
